var uid = getParameter("uid");
var now = new Date();
$(function() {
	charts(now);
});

function charts(now) {
	var urlStr = serverPath + "/app/doctor/diabetes/sugarView.do";
	
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"'+uid+'",'+
	    '	   "method":"showSugarChart",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '			 "befdate":"'+getLastDay(now, 29).format("yyyy-MM-dd")+'",'+
	    '			 "aftdate":"'+getNextDay(now, 1).format("yyyy-MM-dd")+'",'+
	    '			 "day":"30",'+
	    '	      }'+
	    '	   ]'+
	    '	}';
		
	UseAjax(urlStr, jsonStr, document.cookie, rt, '1');
}

function rt(data) {
	var datas = new Array();
	var wi = data.resultlist[0].records;
	var maxsugar = 15;
	if(wi.length > 0){
	for (var int = 0; int < wi.length; int++) {
		datas[int] = [
				Date.UTC(wi[int].dates.substring(0, 4), wi[int].dates.substring(
						5, 7) - 1, wi[int].dates.substring(8, 10), wi[int].dates
						.substring(11, 13)), Number(wi[int].value) ];
		if(Number(wi[int].value)>maxsugar){
			maxsugar =Number(wi[int].value);
		}
	}}
	$('#container').highcharts(
			{

				title : {
					text : ''
				},
				subtitle : {
					text : ''
				},
				xAxis : {
					type : 'datetime',
					dateTimeLabelFormats : {
					    second : '%H:%M:%S',
					    minute : '%H:%M',
					    hour : '%H:%M',
					    day : '%m-%d',
					    week : '%m-%d',
					    month : '%m',
					    year : '%Y' 
					},
					min : Date.UTC(now.getFullYear(), now.getMonth(), now
							.getDate() - 29),
					max : Date.UTC(now.getFullYear(), now.getMonth(), now
							.getDate() + 1),
					maxPadding : 5,
					gridLineWidth : 0.5,
					gridLineDashStyle : 'Dash',// 样式 虚线
					tickPixelInterval : 50
//	                labels:{
//	                    x:45,//调节x偏移
//	                    //y:-35,//调节y偏移
//	                    //rotation:25//调节倾斜角度偏移
//	                 }
				},
				yAxis : {
					labels : {
						step : 1,
						formatter : function() {
							return "" + this.value ;
						}
					},
					tickInterval : parseInt(maxsugar/6),
					min : 0, // 固定列最小值
					max : maxsugar,
					title : {
						//align: 'high',
		                //offset: 10,
		                text: 'mmol/L'
		                //rotation: 0,
		                //y: 25
					} 
					,
					plotBands : [ {// 标示区域
						from : 4.4,
						to : 10.0,
						color : '#F5F5F5'
						/*label:{
						     text:"4.4-10.0",     //标签的内容
						     align:'left',                //标签的水平位置，水平居左,默认是水平居中center
						     x:-20 ,                        //标签相对于被定位的位置水平偏移的像素，重新定位，水平居左10px
						     verticalAlign: "middle",      
						}*/
					} ]
				},
				tooltip : {
					formatter : function() {
						//return 'ceshi';
					}
				},
				plotOptions : {
					line : {
						dataLabels : {
							enabled : true
						},
						marker: {
		                    enabled: true
		                },
						enableMouseTracking : false
					}
				},
				credits : {
					enabled : false
				},
				legend : {
					enabled : false
				},
				series : [ {
					pointPadding : 0,// 数据点之间的距离值
					groupPadding : 0,// 分组之间的距离值
					name : null,
					data : datas
				} ]
			});
}

$(function() {
	// swipe 的dome
	$("#container").swipe(
			{
				swipeLeft : function(event, direction, distance, duration,
						fingerCount) {

					now = getNextDay(now, 30);
					charts( now);
				},
				swipeRight : function(event, direction, distance, duration,
						fingerCount) {
					now = getLastDay(now, 30);
					charts( now);
				}
			});

});